package com.jandn.media.action;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.jandn.common.action.BaseAction;

public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	public String execute() throws Exception {
		String login = (String)this.getSession().get("userId");
		if(login == null || "".equals(login)){
			return "noSession";
		}
		return "loginSuccess";
	}
	
	public String doLogin() throws UnsupportedEncodingException{
		//Map<String, Object> model = PramUtil.getModel(ServletActionContext.getRequest());
		//model = PramUtil.setPage(model);
		if(loginInfoDTO != null){
			if(loginInfoDTO.getUserName()!=null && !"".equals(loginInfoDTO.getUserName())){
				List<ChatDTO> list = ChatUtil.selectFormCache(loginInfoDTO.getUserName(), 0);
				if(list!=null && list.size()!=0){
					getSession().put("uuid",list.get(list.size()-1).getUuid());
				}else{
					getSession().put("uuid",0);
				}
				String name = loginInfoDTO.getUserName();
				if(Arrays.asList(nameArray).contains(name)){
					getSession().put("userId",name);
					getRequest().setAttribute("name",name);
					return "loginSuccess";
				}
			}
		}
		return "noSession";
	}
	
	public String changName() throws Exception {
		return "noSession";
	}
	
	/**
	 * 判断传入的字符串是否在List中存在[极限情况当String=""且list中也存在""返回true]
	 * @param str[String]
	 * @param list[List<String>]
	 * @return boolean
	 */
	public static boolean strIsinList(String str,List list){
		if(list == null || str == null){
			return false;
		}
		Iterator it = list.iterator();
		while(it.hasNext()){
			if(str.equals(it.next())){
				return true;
			}
		}
		return false;
	}
	
	private static String[] nameArray = {"童飞","王利","谢冰","陈祚坤","孔卫佳","岳振庭","刘健","付亮","元景明","刘凯进","何礼根","周正勇","傅强","张均","王少辉","王嗣玮","吴安平","王兵","姜智文","黄斌","刘秋霞","李鹏","黄龙其","王俐","彭杰文","吴恒","黄平强","寇建杰","王惠","李月","熊春花","谭仁棕"};
}
